package com.mscsoft.midi;

import java.io.IOException;
import java.util.Date;
import java.util.Scanner;

import javax.sound.midi.InvalidMidiDataException;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import com.mscsoft.musicscore.*;

public class MusicScoreGenerator {
    private static final Logger logger = LogManager.getLogger(MusicScoreGenerator.class.getSimpleName());

    private static int getMetaID() {
        return -1;
    }

    private static int getSongID() {
        return -1;
    }

    public static void main(String[] args) throws InvalidMidiDataException, IOException {
        Scanner keyin = new Scanner(System.in);
        logger.info("请输入轨道数(2/4/6/8/10k):");
        int column = keyin.nextInt();
        keyin.nextLine();
        while (column != 2 && column != 4 && column != 6 && column != 8 && column != 10) {
            logger.error("非法的轨道数.");
            column = keyin.nextInt();
            keyin.nextLine();
        }
        logger.info("请输入midi文件路径:");
        MidiReader midiReader = new MidiReader(keyin.nextLine(), column);
        midiReader.read();
        double bpm = midiReader.getBpm();
        int metaID = getMetaID();
        logger.info("请输入<level>:");
        int level = keyin.nextInt();
        keyin.nextLine();
        logger.info("请输入<song-title>:");
        String title = keyin.nextLine();
        logger.info("请输入<song-artist>:");
        String artist = keyin.nextLine();
        int songID = getSongID();
        logger.info("请输入<song-img>:");
        String img = keyin.nextLine();
        logger.info("请输入<creator>:");
        String creator = keyin.nextLine();
        long creTime = new Date().getTime();
        logger.info("请输入<offset>(ms):");
        int offset = keyin.nextInt();
        keyin.nextLine();
        logger.info("请输入<soundPath>:");
        String soundPath = keyin.nextLine();

        Music song = new Music(songID, title, artist);
        Meta meta = new Meta(metaID, column, level, song, img, creator, creTime);
        Sound sound = new Sound(offset, soundPath);
        MusicScore ms = new MusicScore(bpm, meta, midiReader.getNotes(), sound);
        MusicScore.Tools.writeToFile(title + ".json", ms);
        logger.info("谱面已成功生成并输出至: {}.json", title);
        keyin.close();
    }
}
